/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iss.basket.bizLogic;

import com.iss.basket.ajaxModels.Status;
import com.iss.basket.models.Customers;
import com.iss.basket.models.UsersGroups;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author yc
 */

@Stateless

public class CustomerService {
     
    @PersistenceContext
    EntityManager em;
    public boolean isCustomerExist(String username)
    {
        boolean result = false;
        String sql = "select c from Customers c where c.username.username=:username";
        Query q = em.createQuery(sql);
        q.setParameter("username", username);
        if(q.getResultList().size()>0)
        {
           result=true;
        }        
        return result;
    }
    public boolean registerCustomer(Customers c)
    {
        boolean result = false;
        String username = c.getUsername().getUsername();
        
        if(isCustomerExist(username))
        {
             
            return result;             
        }
        else 
        {
            //add user
            UsersGroups group = new UsersGroups();
            group.setGroupId("user");
            group.setUsername(username);
            c.getUsername().setUsersGroups(group);            
            
            //-----------persist-----------
            em.persist(c);
            result=true;
            return result;
            //-------------end persis--------
        }
    }
   
}
